package com.Lixiaoqian.CardPlay.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Lixiaoqian.CardPlay.R;
import com.Lixiaoqian.CardPlay.base.BaseActivity;
import com.Lixiaoqian.CardPlay.base.MyApp;
import com.Lixiaoqian.CardPlay.utils.Constant;
import com.Lixiaoqian.CardPlay.utils.DialogUtils;
import com.Lixiaoqian.CardPlay.utils.PrefUtils;
import com.Lixiaoqian.CardPlay.utils.ToastUtils;
import com.Lixiaoqian.CardPlay.utils.TopSnackbarUtil;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class SetActivity extends BaseActivity implements UMAuthListener, View.OnClickListener {

    @BindView(R.id.tv_topbar_center)
    TextView tvTopbarCenter;
    @BindView(R.id.iv_topbar_right)
    ImageView ivTopbarRight;
    @BindView(R.id.toptoolbar)
    Toolbar toptoolbar;
    @BindView(R.id.im_bind_phone)
    ImageView imBindPhone;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_phone_num)
    TextView tvPhoneNum;
    @BindView(R.id.layout_setting_phone)
    LinearLayout layoutSettingPhone;
    @BindView(R.id.im_bind_sina)
    ImageView imBindSina;
    @BindView(R.id.tv_sina)
    TextView tvSina;
    @BindView(R.id.layout_setting_weibo)
    RelativeLayout layoutSettingWeibo;
    @BindView(R.id.tv_weixin)
    TextView tvWeixin;
    @BindView(R.id.im_bind_weixin)
    ImageView imBindWeixin;
    @BindView(R.id.layout_setting_weixin)
    RelativeLayout layoutSettingWeixin;
    @BindView(R.id.linearLayout1)
    LinearLayout linearLayout1;
    @BindView(R.id.item_about)
    RelativeLayout itemAbout;
    @BindView(R.id.tv_chace_size)
    TextView tvChaceSize;
    @BindView(R.id.item_huancun)
    RelativeLayout itemHuancun;
    @BindView(R.id.item_update)
    RelativeLayout itemUpdate;
    @BindView(R.id.btn_tuichu)
    Button btnTuichu;
    @BindView(R.id.tv_setting_version)
    TextView tvSettingVersion;

    private String phonenumber;
    private String wechatOpenId;
    private String weiboOpenId;
    private HashMap<String, String> hashMap;
    private int user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindData();
        initEvent();
    }

    @Override
    protected void initToolbar() {
        super.initToolbar();
        actionBar.setTitle("设置");
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_set;
    }

    public static void launch(Activity fromActivity) {
        fromActivity.startActivity(new Intent(fromActivity, SetActivity.class));
    }


    /**
     * ui绑定数据
     */
    private void bindData() {
        //获取缓存大小
        user_id = PrefUtils.getInt(Constant.PRE_USER_ID, -1);
        try {
            tvChaceSize.setText(MyApp.getCacheSize(this.getCacheDir()));
        } catch (Exception e) {
            tvChaceSize.setText(0 + "");
        }
        //得到当前版本号
        tvSettingVersion.setText(MyApp.getVersion());
        //初始化绑定账号
        bindPlarmt();
    }

    private void initEvent() {
        itemAbout.setOnClickListener(this);
        itemHuancun.setOnClickListener(this);
        itemUpdate.setOnClickListener(this);
        btnTuichu.setOnClickListener(this);
    }

    /**
     * 账号绑定点击事件
     *
     * @param view
     */
    public void shareClick(View view) {
        if (MyApp.isLogin()) {
            hashMap = new HashMap<>();
            DialogUtils.showCustomDialogFragment(this);
            switch (view.getId()) {
                case R.id.layout_setting_phone:
                    if (phonenumber.equals("")) {
                        LoginActivity.launch(this);
                    } else {
                        hashMap.put("phonenumber", "");
                        unBindPlarmApi(hashMap);
                    }
                    break;
                case R.id.layout_setting_weibo:
                    if (weiboOpenId.equals("")) {
                        doOauth(SHARE_MEDIA.SINA);
                    } else {
                        hashMap.put("weiboOpenId", weiboOpenId);
                        unBindPlarmApi(hashMap);
                    }
                    break;
                case R.id.layout_setting_weixin:
                    if (wechatOpenId.equals("")) {
                        doOauth(SHARE_MEDIA.WEIXIN);
                    } else {
                        hashMap.put("wechatOpenId", wechatOpenId);
                        unBindPlarmApi(hashMap);
                    }
                    break;
              /*  case R.id.layout_setting_qq:
                    if (qqOpenId.equals("")) {
                        doOauth(SHARE_MEDIA.QQ);
                    } else {
                        hashMap.put("qqOpenId", qqOpenId);
                        unBindPlarmApi(hashMap);
                    }
                    break;*/
            }
        } else {
            ToastUtils.showShort(this, "请先登录");
        }

    }

    /**
     * 解除所有绑定状态
     */
    private void unbindPlarmtState() {
        layoutSettingPhone.setSelected(false);
        tvPhone.setText("手机未解绑");
       /* layoutSettingQq.setSelected(false);
        tvQq.setText("QQ未解绑");*/
        layoutSettingWeixin.setSelected(false);
        tvWeixin.setText("微信未解绑");
        layoutSettingWeibo.setSelected(false);
        tvSina.setText("新浪未解绑");
    }


    /**
     * 更新绑定登录状态
     */
    private void bindPlarmt() {
        if (!MyApp.isLogin()) {
            return;
        }
        getOpenID();
        if (!phonenumber.equals("")) {
            layoutSettingPhone.setSelected(true);
            tvPhone.setText("手机已绑定");
        } else {
            layoutSettingPhone.setSelected(false);
            tvPhone.setText("手机未解绑");
        }
       /* if (!qqOpenId.equals("")) {
            layoutSettingQq.setSelected(true);
            tvQq.setText("QQ已经绑定");
        } else {
            layoutSettingQq.setSelected(false);
            tvQq.setText("QQ未解绑");
        }*/
        if (!wechatOpenId.equals("")) {
            layoutSettingWeixin.setSelected(true);
            tvWeixin.setText("微信已经绑定");
        } else {
            layoutSettingWeixin.setSelected(false);
            tvWeixin.setText("微信未解绑");
        }
        if (!weiboOpenId.equals("")) {
            layoutSettingWeibo.setSelected(true);
            tvSina.setText("微博已经绑定");
        } else {
            layoutSettingWeibo.setSelected(false);
            tvSina.setText("新浪未解绑");
        }
    }

    /**
     * 各个平台的openid值
     */
    private void getOpenID() {
        phonenumber = PrefUtils.getString(Constant.PRE_USER_PHONENUMBER, "");
        //qqOpenId = PrefUtils.getString(Constant.PRE_USER_QQOPENID, "");
        wechatOpenId = PrefUtils.getString(Constant.PRE_USER_WECHATOPENID, "");
        weiboOpenId = PrefUtils.getString(Constant.PRE_USER_WEIBOOPENID, "");
    }

    /**
     * 绑定账号
     *
     * @param plant
     */
    private void doOauth(SHARE_MEDIA plant) {
        UMShareAPI mShareAPI = UMShareAPI.get(this);
        mShareAPI.doOauthVerify(this, plant, this);
    }

    /**
     * 退出登录
     */
    private void quitLogin(View view) {

        if (MyApp.isLogin()) {
            DialogUtils.showNotitleDialog(this, "是否退出登录?", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    PrefUtils.putBoolean(Constant.PRE_USER_ISLOGIN, false);
                    ToastUtils.showLong(SetActivity.this, "成功退出登录");
                    unbindPlarmtState();
                }

            });
        } else {
            TopSnackbarUtil.showTopBar("您还未登录");
        }
    }


    /**
     * 版本更新
     */
    private void updataVersion() {

    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
        String openid = map.get("openid");

        //后台绑定到服务器
        bindPlarmApi(openid, share_media);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

    }

    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {

    }

    /**
     * 绑定到服务器
     *
     * @param openid
     * @param share_media
     */
    private void bindPlarmApi(String openid, SHARE_MEDIA share_media) {
    }

    /**
     * 解绑账号上传服务器
     *
     * @param map
     */
    private void unBindPlarmApi(Map<String, String> map) {

    }

    /**
     * item的点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_about:
                AboutActivity.launch(this);
                break;
            case R.id.item_huancun:
                DialogUtils.showNotitleDialog(this, "是否清理缓存?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        MyApp.cleanInternalCache(SetActivity.this);
                        tvChaceSize.setText("0MB");
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.item_update:
                updataVersion();
                break;
            case R.id.btn_tuichu:
                quitLogin(v);
                break;
        }
    }
}
